package edu.swust.graduation_project.eauction.activity

import android.content.SharedPreferences
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import android.widget.Toast
import androidx.core.content.ContentProviderCompat.requireContext
import edu.swust.graduation_project.eauction.R
import edu.swust.graduation_project.eauction.fragment.UserFragment
import java.sql.Connection
import java.sql.DriverManager

class EditUserActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_edit_user)
        val mEtUserPhone=findViewById<EditText>(R.id.et_edit_userphone)
        val mEtUserEmail=findViewById<EditText>(R.id.et_edit_userEmail)
        val mBtnEdit=findViewById<Button>(R.id.btn_edit)
        val mSharedPreferences: SharedPreferences = this.getSharedPreferences(
            "user",
            AppCompatActivity.MODE_PRIVATE
        )
        val userName=mSharedPreferences.getString("username", "")
        mBtnEdit.setOnClickListener {
            val userPhone=mEtUserPhone.text.toString().trim()
            val userEmail=mEtUserEmail.text.toString().trim()
            Log.d("userPhone",userPhone)
            object :Thread(){
                override fun run() {
                    val UpdateSQL =
                       "    UPDATE `test_case`.`Account` SET `user_phone` = " +
                               "'$userPhone'" +
                               ", `user_email` = " +
                               "'$userEmail'" +
                               " WHERE `Account`.`user_name` = '$userName'"
                    val cn: Connection = DriverManager.getConnection(
                        "*",
                        "*", "*"
                    )
                    val ps = cn.createStatement()
                    ps.execute(UpdateSQL)
                    ps.close()
                    cn.close()
                    finish()
                    Log.d("EditUser", "上传成功")
                }
            }.start()

        }
    }
}